package com.peoit.honeycomb.application.impl;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import com.peoit.honeycomb.domain.HoneycombProject;
import com.peoit.honeycomb.dto.ProjectQueryDto;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.peoit.honeycomb.application.InformationApplication;
import com.peoit.honeycomb.domain.Information;
import com.peoit.honeycomb.domain.repository.HibernateEntityRepository;
import com.peoit.honeycomb.domain.util.CommonUtil;
import com.peoit.honeycomb.dto.InformationDto;
import com.peoit.honeycomb.dto.InformationQueryDto;

@Service
public class InformationApplicationImpl extends HibernateEntityRepository implements InformationApplication {

	public List<Information> findInformations(InformationQueryDto dto) {
		StringBuffer where = new StringBuffer();
		List<Object> params = new ArrayList<>();
		if (!StringUtils.isEmpty(dto.getTitle())) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			where.append(" title like ? ");
			params.add("%" + dto.getTitle() + "%");

		}
		if (!StringUtils.isEmpty(dto.getContent())) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			where.append(" title content ? ");
			params.add("%" + dto.getContent() + "%");

		}
		return queryByWherePage(Information.class, where.toString(), CommonUtil.listToArray(params), dto.getOffset(),
				dto.getPageSize());
	}

	@Override
	@Transactional
	public boolean addInformation(InformationDto dto) {
		if (dto.getId().equals("") || StringUtils.isEmpty(dto.getId())) {
			create(InformationDto.toEntity(dto));
		} else {
			update(InformationDto.toEntity(dto));
		}
		return true;
	}

	/**
	 * 根据条件返回 总条数
	 *
	 * @param dto
	 * @return
	 */
	@Override
	public long findByWhereCount(InformationQueryDto dto) {


		Object[] params = {dto.getTitle(),dto.getContent()};
		StringBuffer where = new StringBuffer("");
		if (!StringUtils.isEmpty(dto.getTitle())) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			where.append(" title = ? ");
		}
		if (!StringUtils.isEmpty(dto.getContent())) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			where.append(" content = ? ");
		}


		long result;
		if (StringUtils.isEmpty(where.toString())) {
			result = getCountByWhere(Information.class);

		} else {
			result = getCountByWhere(Information.class, where.toString(), params);
		}
		return result;
	}

	@Override
	@Transactional
	public boolean updateInformation(InformationDto dto) {
		update(InformationDto.toEntity(dto));
		return true;
	}

	@Override

	@Transactional
	public boolean deleteInformation(String id) {
		String sql = "delete from Information where id = ?";
		Object[] params = { id };
		this.updateByFullJPQL(sql, params);
		return true;
	}

	@Override
	public InformationDto informationDetail(String id) {
		return InformationDto.toDto(find(Information.class, id));
	}

}